/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.service;

import java.util.ArrayList;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.dao.ModuleAuthDao;
import com.tech15.core.model.ModuleAuth;
import com.tech15.core.vo.query.ModuleAuthQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class ModuleAuthManager extends BaseManager<ModuleAuth,Long>{

	private ModuleAuthDao moduleAuthDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setModuleAuthDao(ModuleAuthDao dao) {
		this.moduleAuthDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.moduleAuthDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(ModuleAuthQuery query) {
		return moduleAuthDao.findPage(query);
	}
	
	public void show(ModuleAuth modAuth){
		List<ModuleAuth> list = moduleAuthDao.findByMdid(modAuth.getMdid());
		for(ModuleAuth ma : list){
			if("1".equals(ma.getAuthType())){
				modAuth.getAuthUsers().add(ma);
			}
			if("2".equals(ma.getAuthType())){
				modAuth.getAuthGroups().add(ma);
			}
		}
	}
	/**
	 * 保存模块权限信息
	 * @param modAuth
	 */
	public void auth(ModuleAuth modAuth) {
		List<ModuleAuth> list = new ArrayList<ModuleAuth>();
		/*处理用户*/
		if(!ObjectUtils.isNullOrEmptyString(modAuth.getUserIds())){
			/*删除去掉的用户*/
			moduleAuthDao.delete("delete sys_module_auth t where t.auth_type=1 and t.mdid="+modAuth.getMdid()+" and t.authid not in ("+modAuth.getUserIds()+")");
			String[] userIds = modAuth.getUserIds().split(",");
			for(String id : userIds){
				ModuleAuth ma = new ModuleAuth();
				ma.setMdid(modAuth.getMdid());
				ma.setAuthid(new Long(id));
				ma.setAuthType("1");
				boolean res = moduleAuthDao.isUnique(ma, "mdid,authid,authType");
				if(res){
					list.add(ma);
				}
			}
		}else{
			moduleAuthDao.delete("delete sys_module_auth t where t.auth_type=1 and t.mdid="+modAuth.getMdid());
		}
		/*处理组*/
		if(!ObjectUtils.isNullOrEmptyString(modAuth.getGroupIds())){
			/*删除去掉的组*/
			moduleAuthDao.delete("delete sys_module_auth t where t.auth_type=2 and t.mdid="+modAuth.getMdid()+" and t.authid not in ("+modAuth.getGroupIds()+")");
			String[] groupIds = modAuth.getGroupIds().split(",");
			for(String id : groupIds){
				ModuleAuth ma = new ModuleAuth();
				ma.setMdid(modAuth.getMdid());
				ma.setAuthid(new Long(id));
				ma.setAuthType("2");
				boolean res = moduleAuthDao.isUnique(ma, "mdid,authid,authType");
				if(res){
					list.add(ma);
				}
			}
		}else{
			moduleAuthDao.delete("delete sys_module_auth t where t.auth_type=2 and t.mdid="+modAuth.getMdid());
		}
		moduleAuthDao.saveAll(list);
	}
	public void perAuth(String chkper,Long authid,String authType){
		List<ModuleAuth> list = new ArrayList<ModuleAuth>();
		if(!ObjectUtils.isNullOrEmptyString(chkper)){
			moduleAuthDao.delete("delete sys_module_auth t where t.authid="+authid+" and t.auth_type="+authType+" and t.mdid not in ("+chkper+")");
			String[] mdids = chkper.replace(" ", "").split(",");
			for(String id : mdids){
				ModuleAuth ma = new ModuleAuth();
				ma.setMdid(new Long(id));
				ma.setAuthid(new Long(authid));
				ma.setAuthType(authType);
				boolean res = moduleAuthDao.isUnique(ma, "mdid,authid,authType");
				if(res){
					list.add(ma);
				}
			}
		}else{
			moduleAuthDao.delete("delete sys_module_auth t where t.authid="+authid+" and t.auth_type="+authType);
		}
		moduleAuthDao.saveAll(list);
	}
	
}
